<template>
	<view>
		<view class="head">
			<view class="head_left" @tap="back()">
				<image src="../../../static/wode/return.png" mode=""></image>
				<text>返回</text>
			</view>
			<view class="head_mid">
				城市
			</view>
			<view class="head_left">

			</view>
		</view>

		<view class="countryBox">
			<view class="inputBox">
				<input type="text" v-model="countryName" placeholder="搜索省份或州" @input="searchCountry" />
			</view>
			<view class="marginTop">
				<view v-if="isShow" class="companyBox height fontSize" v-for="(item,index) in provinceLists" :key="index" v-text="item.Name"
				 @tap="selsectProvince(item.Name)"></view>
				<view v-if="isDisplay" class="companyBox height fontSize" v-text="countrys" @tap="selsectProvince(countrys)"></view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		Location
	} from '../../../common/worldCity.js'
	export default {
		data() {
			return {
				countryName: "",
				countryLists: Location.CountryRegion, //国家列表
				provinceLists: [], //省份列表
				isShow: true,
				isDisplay: false,
				country: "",
				countrys: "",
				isNativePlace: false, //判断是选择籍贯还是所在地
				isHometown: false,
				isJiguan:false
			};
		},
		onLoad(option) {
			this.country = option.country
			this.isNativePlace = option.isNativePlace //从上一个页面传入判断是选择籍贯还是所在地
			this.isHometown = option.isHometown
			this.isJiguan=option.isJiguan
			//根据国家往下查询（省份、城市）
			for (var i = 0; i < this.countryLists.length; i++) {
				if (this.countryLists[i].Name == this.country) {
					if (this.countryLists[i].State) { //如果有省份
						if (this.countryLists[i].State.City) {
							this.provinceLists = this.countryLists[i].State.City //城市
						} else {
							this.provinceLists = this.countryLists[i].State //省份
						}
					} else {
						this.provinceLists = this.countryLists[i].City //城市
					}
				}
			}
		},
		methods: {
			searchCountry() {
				if (this.countryName == '') {
					this.isShow = true
				} else {
					for (var i = 0; i < this.provinceLists.length; i++) {
						if (this.countryName == this.provinceLists[i].Name) {
							this.isShow = false
							this.isDisplay = true
							this.countrys = this.provinceLists[i].Name
						}
					}
				}
			},
			selsectProvince(province) { //根据国家查询城市或省份
				// uni.setStorageSync('province',province)
				console.log(province)
				for (var i = 0; i < this.provinceLists.length; i++) {
					if (province == this.provinceLists[i].Name) {
						if (this.provinceLists[i].City) { //如果有城市的话
							uni.redirectTo({
								url: "/pages/My/cityLists/cityLists?province=" + province + "&country=" + this.country + '&isNativePlace=' +
									this.isNativePlace + '&isJiguan=' +this.isJiguan
							})
							console.log(i)
						} else {

							if (this.isNativePlace == 'true') { //返回选择的国家并返回是籍贯还是所在地
								uni.removeStorageSync('city');

								uni.setStorageSync('countryName', this.country)
								uni.setStorageSync('province', province)
								var me = this;
								var data = {
									user_id: uni.getStorageSync('user_id'),
									hometown: this.country + '.' + province
								}
								if (this.isHometown) {
									this.myAjaxNewPost({
										model: 'user',
										controllerName: 'userInfo',
										actionName: 'update',
										data: data,
										successBack: function(res) {
											console.log(res, '更新故乡');
										}
									})
								}
								if (this.isJiguan) {
									this.myAjaxNewPost({
										model: 'user',
										controllerName: 'overseasIdentity',
										actionName: 'update',
										data: {
											user_id: uni.getStorageSync('user_id'),
											native_place: me.country + '.' + this.province
										},
										successBack: function(res) {
											console.log(res, '更新籍贯');
										}
									})
								}


							} else { //所在地
								uni.removeStorageSync('chengshi');

								uni.setStorageSync('guojia', this.country)
								uni.setStorageSync('shengfen', province)
								uni.setStorageSync('isProvince', true)

								var data = {
									city: '',
									country: '',
									user_id: '',
								}
								data.country = uni.getStorageSync('guojia', this.country)
								data.city = uni.getStorageSync('shengfen', province)
								data.user_id = uni.getStorageSync('user_id')
								this.myAjaxNewPost({
									model: 'user',
									controllerName: 'userInfo',
									actionName: 'update',
									data: data,
									successBack: function(res) {
										console.log(res, '更新所在地');
									}
								})
							}
							uni.navigateBack({
								delta: 2
							})
						}
					}
				}

			},
			back() { //返回
				uni.navigateBack({
					delta: 1
				})
			},
		}

	}
</script>

<style scoped>
	.head {
		width: 100%;
		display: flex;
		justify-content: space-between;
		height: 162upx;
		background: #ffffff;
		padding: 0 24upx;
		z-index: 200;
		position: fixed;
	}

	.head image {
		width: 20upx;
		height: 36upx;
		margin-right: 9upx;
	}

	.head_left {
		height: 82upx;
		width: 106upx;
		display: flex;
		align-items: center;
		line-height: 82upx;
		color: #666666;
		font-size: 32upx;
		padding-top: 70upx;
	}
	.head_mid{
		padding-top: 90upx;
	}

	.head_right {
		width: 150upx;
	}

	.companyBox {
		display: flex;
		align-items: center;
		border-bottom: 1upx solid #e4e4e4
	}

	.companyBox.height {
		height: 90upx;
		background-color: white;
		padding: 0 40upx;
		box-sizing: border-box;
	}

	.fontSize {
		font-size: 30upx;
	}

	.inputBox {
		width: 100%;
		height: 90upx;
		display: flex;
		position: fixed;
		padding: 20upx 40upx 0 40upx;
		box-sizing: border-box;
		border-bottom: 1rupx solid #e4e4e4;
		z-index: 111;
		text-align: center;
		background: #f5f5f5;
		margin-top: 163upx;
	}

	.marginTop {
		position: relative;
		top: 90upx;
		z-index: 11;
		padding-top: 160upx;
	}

	.inputBox input {
		width: 100%;
		height: 50upx;
		font-size: 30upx;
		border: 1upx solid #E4E4E4;
		border-radius: 50upx;
		padding-left: 20upx;
	}
</style>
